import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../views/Login.vue'
import Register from '../views/Register'
import User from '../views/User.vue'
import Edit from '../views/Edit.vue'
import UserFollows from '../views/UserFollows.vue'
import UserComments from '../views/UserComments.vue'
import MyStar from '../views/MyStar.vue'
Vue.use(VueRouter)
const router = new VueRouter({
  routes: [
    { path: '/mystar', component: MyStar, name: 'mystar' },
    { path: '/login', component: Login, name: 'login' },
    { path: '/userfollows', component: UserFollows, name: 'userfollows' },
    { path: '/register', component: Register, name: 'register' },
    { path: '/user', component: User, name: 'user' },
    { path: '/edit', component: Edit, name: 'edit' },
    { path: '/usercomments', component: UserComments, name: 'usercomments' },
    { path: '/', redirect: '/login' }
  ]
})
router.beforeEach((to, from, next) => {
  if (to.path == '/user') {
    const token = localStorage.getItem('token')
    if (token) {
      next()
    } else {
      next('/login')
    }
  } else {
    next()
  }
})
export default router